package com.woniuxy.background.service.impl;


import com.woniuxy.background.constant.SysConstant;
import com.woniuxy.background.dao.MenuMapper;
import com.woniuxy.background.dao.RoleMenuMapper;
import com.woniuxy.background.dto.MenuDTO;
import com.woniuxy.background.exception.MenusServiceException;
import com.woniuxy.background.pojo.Admin;
import com.woniuxy.background.pojo.Menu;
import com.woniuxy.background.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MenusServiceImpl implements MenusService {

    @Autowired
    private MenuMapper menusMapper;

    @Autowired
    private RoleMenuMapper rolesMenusMapper;

    @Override
    public List<MenuDTO> findByAdmin(Admin admin) {
        //查询所有菜单节点数据根据登录用的角色Id
        List<Menu> list = menusMapper.findByRoleId(admin.getRoleId());
        List result = new ArrayList();
        for (Menu menus : list) {
            //如果是根节点：父节点id的值等于0的节点
            if(menus.getParentId().equals(SysConstant.MENUS_ROOT)){
                //封装菜单根节点数据
                MenuDTO dto = new MenuDTO();
                dto.setTitle(menus.getTitle());
                dto.setSpread(menus.getSpread());
                dto.setIcon(menus.getIcon());
                dto.setHref(menus.getHref());
                List childrens = new ArrayList();
                //查找menus节点下面的 子节点
                for (Menu childrenMenu : list) {
                    if(childrenMenu.getParentId().equals(menus.getMenuId())){
                        //封装子节点数据
                        MenuDTO child = new MenuDTO();
                        child.setTitle(childrenMenu.getTitle());
                        child.setHref(childrenMenu.getHref());
                        child.setIcon(childrenMenu.getIcon());
                        child.setSpread(childrenMenu.getSpread());
                        childrens.add(child);
                        dto.setChildren(childrens);//添加子节点
                    }
                }
                result.add(dto);
            }
        }

        return result;
    }

    @Override
    public List<Menu> findAll() {
        return menusMapper.selectAll();
    }

    @Override
    public List<Menu> findCheckMeuns(int roleId) {
        //查询所有菜单数据
        List<Menu> menus = menusMapper.selectAll();
        //查询当前角色默认的权限数据
        List<Menu> defaultMenus = menusMapper.findByRoleId(roleId);
        for (Menu menu : menus) {
            for (Menu defaultMenu: defaultMenus) {
                if(menu.getMenuId().equals(defaultMenu.getMenuId())){
                    menu.setChecked("true");
                }
            }
        }
        return menus;
    }

    @Override
    public boolean checkMenuName(Menu menus) {
        List<Menu> list = menusMapper.findByTitle(menus.getTitle());
        if(list == null || list.size()==0){
            return false;//不存在
        }
        for (Menu m : list) {
            if(m.getParentId().equals(menus.getParentId())){
                return true;//在同级节点下有相同名称的节点
            }
        }
        return false;
    }



    @Override
    public void saveMeuns(Menu menu) {
        menu.setSpread("false");
        menusMapper.insertSelective(menu);
    }

    @Override
    public void updateSorting(int menuId, int sorting) {
        menusMapper.updateSorting(menuId,sorting);
    }

    @Override
    public boolean checkChildrenNode(int id) {
        List<Menu> menus = menusMapper.selectByParentId(id);

        return menus==null || menus.size()==0?false:true;
    }

    @Override
    public void removeMenus(int id) {
        //查看当前菜单是否被角色占用
        List<Menu> menus = rolesMenusMapper.selectByMeunId(id);
        if(menus != null && menus.size()>0){
            throw new MenusServiceException("该节点被角色使用，不能删除");
        }
        menusMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Menu record) {
        return menusMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Menu selectByPrimaryKey(Integer menuId) {
        return menusMapper.selectByPrimaryKey(menuId);
    }
}
